package com.ecmcloud.flow.utils;

import java.util.List;

import com.ecmcloud.common.dto.ProcessInstanceAssignUserRecordParamDto;
import com.ecmcloud.common.dto.ProcessInstanceCopyDto;
import com.ecmcloud.common.dto.ProcessInstanceNodeRecordParamDto;
import com.ecmcloud.common.dto.ProcessInstanceParamDto;
import com.ecmcloud.common.dto.ProcessInstanceRecordParamDto;
import com.ecmcloud.common.dto.ProcessNodeDataDto;
import com.ecmcloud.common.dto.Result;
import com.ecmcloud.common.dto.third.DeptDto;
import com.ecmcloud.common.dto.third.MessageDto;
import com.ecmcloud.common.utils.JsonMapperUtils;
import com.ecmcloud.flow.config.FlowProperties;
import com.ecmcloud.flow.constants.BizConstants;
import com.fasterxml.jackson.core.type.TypeReference;
import com.yomahub.tlog.hutoolhttp.TLogHutoolhttpInterceptor;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.http.HttpRequest;

public class BizHttpUtil {

    private static TLogHutoolhttpInterceptor tLogHutoolhttpInterceptor = new TLogHutoolhttpInterceptor();

    @SuppressWarnings("unchecked")
	public static String post(Object object, String url) {
    	FlowProperties flowProperties = SpringUtil.getBean(FlowProperties.class);
        String bizUrl = flowProperties.getBizUrl();
        String post = HttpRequest.post(StrUtil.format("{}{}", bizUrl, url)).body(JsonMapperUtils.toJsonString(object)).addInterceptor(tLogHutoolhttpInterceptor).execute().body();
        return post;
    }


    public static <T> Result<List<T>> postArray(Object object, String url) {
        String jsonString = post(object, url);
        Result<List<T>> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<List<T>>>() {});
        return r;
    }

    @SuppressWarnings("unchecked")
	public static String get(String url) {
    	FlowProperties flowProperties = SpringUtil.getBean(FlowProperties.class);
        String bizUrl = flowProperties.getBizUrl();
        return HttpRequest.get(StrUtil.format("{}{}", bizUrl, url)).addInterceptor(tLogHutoolhttpInterceptor).execute().body();
    }


    /**
     * 节点开始事件
     *
     * @param nodeRecordParamDto
     */
    public static void startNodeEvent(ProcessInstanceNodeRecordParamDto nodeRecordParamDto) {
        post(nodeRecordParamDto, BizConstants.START_NODE_EVENT);
    }

    /**
     * 节点结束事件
     *
     * @param nodeRecordParamDto
     */
    public static void endNodeEvent(ProcessInstanceNodeRecordParamDto nodeRecordParamDto) {
        post(nodeRecordParamDto, BizConstants.END_NODE_EVENT);
    }

    /**
     * 流程结束事件
     *
     * @param processInstanceParamDto
     */
    public static void endProcessEvent(ProcessInstanceParamDto processInstanceParamDto) {
        post(processInstanceParamDto, BizConstants.END_PROCESS);
    }

    /**
     * 保存消息
     *
     * @param messageDto
     */
    public static void saveMessage(MessageDto messageDto) {
        post(messageDto, BizConstants.SAVE_MESSAGE);
    }

    /**
     * 流程结束事件
     *
     * @param processInstanceParamDto
     */
    public static void createProcessEvent(ProcessInstanceRecordParamDto processInstanceParamDto) {
        post(processInstanceParamDto, BizConstants.CREATE_PROCESS_EVENT);
    }

    /**
     * 根据角色id集合查询用户id集合
     *
     * @param roleIdList
     */
    public static Result<List<String>> queryUserIdListByRoleIdList(List<String> roleIdList) {
        return postArray(roleIdList, BizConstants.QUERY_USERIDLIST_BY_ROLEIDLIST);
    }

    /**
     * 根据部门id集合查询所有的用户id集合
     *
     * @param deptIdList
     */
    public static Result<List<String>> queryUserIdListByDepIdList(List<String> deptIdList) {
        String jsonString = post(deptIdList, BizConstants.QUERY_USERIDLIST_BY_DEPIDLIST);
        Result<List<String>> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<List<String>>>() {});
        return r;
    }

    /**
     * 根据用户id查询上级部门id集合
     *
     * @param userId 用户id
     */
    public static Result<List<com.ecmcloud.common.dto.third.DeptDto>> queryParentDepListByUserId(String userId) {
        String jsonString = get(BizConstants.formatByStrTemplate(BizConstants.QUERY_PARENTDEPLIST_BY_USERID, userId));
        Result<List<DeptDto>> r = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<List<DeptDto>>>() {});
        return r;
    }
    /**
     * 查询部门列表
     *
     * @param deptIdList
     */
    public static List<DeptDto> queryDeptList(List<String> deptIdList) {
        String jsonString = post(deptIdList, BizConstants.QUERY_DEPTLIST);
        Result<List<DeptDto>> listR = JsonMapperUtils.getInstance().parseObject(jsonString, new TypeReference<Result<List<DeptDto>>>() {});
        return listR.getData();
    }

    /**
     * 查询流程管理员
     *
     * @param flowId 流程id
     */
    public static Result<String> queryProcessAdmin(String flowId) {
        String s = get(BizConstants.formatByStrTemplate(BizConstants.QUERY_PROCESS_ADMIN, flowId));
        Result<String> longR = JsonMapperUtils.getInstance().parseObject(s, new TypeReference<Result<String>>() {});
        return longR;
    }

    /**
     * 查询节点数据
     *
     * @param flowId
     * @param nodeId
     * @return
     */
    public static Result<String> queryNodeOriData(String flowId, String nodeId) {
        String s = get(BizConstants.formatByStrTemplate(BizConstants.GET_NODEDATA, flowId, nodeId));
        Result<String> r = JsonMapperUtils.getInstance().parseObject(s, new TypeReference<Result<String>>() {});
        return r;
    }

    /**
     * 节点开始指派用户了
     *
     * @param processInstanceAssignUserRecordParamDto
     * @return
     */
    public static void startAssignUser(ProcessInstanceAssignUserRecordParamDto processInstanceAssignUserRecordParamDto) {
        post(processInstanceAssignUserRecordParamDto, BizConstants.START_ASSIGN_USER);
    }

    /**
     * 任务结束事件
     *
     * @param processInstanceAssignUserRecordParamDto
     * @return
     */
    public static void taskEndEvent(ProcessInstanceAssignUserRecordParamDto processInstanceAssignUserRecordParamDto) {
        post(processInstanceAssignUserRecordParamDto, BizConstants.TASK_END_EVENT);
    }

    /**
     * 保存抄送数据
     *
     * @param processInstanceCopyDto
     * @return
     */
    public static void saveCC(ProcessInstanceCopyDto processInstanceCopyDto) {
        post(processInstanceCopyDto, BizConstants.SAVE_CC);
    }

    /**
     * 保存节点原始数据
     *
     * @param processNodeDataDto
     * @return
     */
    public static void saveNodeOriData(ProcessNodeDataDto processNodeDataDto) {
        post(processNodeDataDto, BizConstants.SAVE_NODE_DATA);
    }
}
